﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class ModifyWebConfig : Form
    {
        public ModifyWebConfig()
        {
            InitializeComponent();
            this.FillServerComboBox();
        }

                SQLConnection sqlConnection = new SQLConnection();
        WebConfigParser webConfigParser = new WebConfigParser();
        List<string> serverConnectionStringList = new List<string>();
        List<string> databasesList = new List<string>();

        private void FillServerComboBox()
        {
            this.serverConnectionStringList = sqlConnection.GetServerComboBoxFromFile("serverConnection.txt");

            foreach (string serverConnectionStringListItem in serverConnectionStringList)
            {
                serverCSComboBox.Items.Add(serverConnectionStringListItem);
            } 
        }

        /// <summary>
        /// Otwiera okno dialogowe openFileDialog by znaleźć plik webconfig.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browseWebConfigFileButton_Click(object sender, EventArgs e)
        {
            browseWebConfigDialog.Filter = "Text Files (.config)|*.config|All Files (*.*)|*.*";
            browseWebConfigDialog.FilterIndex = 1;
            browseWebConfigDialog.Multiselect = false;
            browseWebConfigDialog.RestoreDirectory = true;

            if (browseWebConfigDialog.ShowDialog() == DialogResult.OK)
            {
                webConfigPathTextBox.Clear();
                webConfigPathTextBox.Text = System.IO.Path.GetFullPath(browseWebConfigDialog.FileName);
                browseWebConfigDialog.Dispose();
            }
        }

        /// <summary>
        /// Wybór connection string serwera z dostępnej listy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void serverCSComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            sqlConnection.serverConnectionString = serverCSComboBox.SelectedItem.ToString();
            this.databasesList = sqlConnection.GetDatabasesComboBox();
            databasesComboBox.Text = string.Empty;
            databasesComboBox.Items.Clear();

            foreach (string databasesListItem in databasesList)
            {
                databasesComboBox.Items.Add(databasesListItem);
            } 
        }

        /// <summary>
        /// Wybór bazy danych z dostępnej listy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void databasesComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (databasesComboBox.SelectedIndex != -1)
            {
                string databaseName = databasesComboBox.SelectedItem.ToString();
                webConfigParser.GetDatabaseName(databaseName);
            }
        }

        /// <summary>
        /// Zapisz zmiany do pliku web.config
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveWebConfigButton_Click(object sender, EventArgs e)
        {
            if (webConfigPathTextBox.Text != string.Empty && (devAuthRadioButton.Checked != false || osRadioButton.Checked != false) && databasesComboBox.SelectedItem != null)
            {
                webConfigParser.ReadFromFile(webConfigPathTextBox.Text, devAuthRadioButton.Checked);
                this.Close();
            }
            else if (webConfigPathTextBox.Text == string.Empty)
            {
                MessageBox.Show("Lack of web.config path!");
            }
            else if (devAuthRadioButton.Checked == false && osRadioButton.Checked == false)
            {
                MessageBox.Show("Authentication not chosen!");
            }
            else if (databasesComboBox.SelectedItem == null)
            {
                MessageBox.Show("Database not chosen");
            }
        }

        /// <summary>
        /// Kliknij w radioButton developerska autentykacja.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void devAuthRadioButton_Click(object sender, EventArgs e)
        {
            devAuthRadioButton.Checked = true;
            osRadioButton.Checked = false;
        }

        /// <summary>
        /// Kliknij w radioButton os autentykacja.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void osRadioButton_Click(object sender, EventArgs e)
        {
            devAuthRadioButton.Checked = false;
            osRadioButton.Checked = true;
        }

        private void editServerCSButton_Click(object sender, EventArgs e)
        {
            //webConfigPanel.Hide();
            serverConnectionStringEditPanel.Location = new Point(9, 12);
            serverConnectionStringEditPanel.Visible = true;
            webConfigPanel.Visible = false;
            serverConnectionStringsRichTextBox.LoadFile("serverConnection.txt", RichTextBoxStreamType.PlainText);
        }

        private void saveServerConnectionString_Click(object sender, EventArgs e)
        {
            serverConnectionStringEditPanel.Visible = false;
            webConfigPanel.Visible = true;
            serverConnectionStringsRichTextBox.SaveFile("serverConnection.txt", RichTextBoxStreamType.PlainText);
            databasesComboBox.Text = string.Empty;
            serverCSComboBox.Text = string.Empty;
            serverCSComboBox.Items.Clear();
            databasesComboBox.Items.Clear();
            FillServerComboBox();
        }

        private void cancelServerConnectionString_Click(object sender, EventArgs e)
        {
            serverConnectionStringEditPanel.Visible = false;
            webConfigPanel.Visible = true;
        }

        private void ModifyWebConfig_Load(object sender, EventArgs e)
        {

        }
    }
}
